﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Data;

namespace SharePointHNSC.ApplicationPages
{
    public partial class managedpath : DialogAdminPageBase
    {
        protected const string InclusionColumnName = "Inclusion";
        protected const string PathNameColumnName = "PathName";
        protected const string CommentColumnName = "Comment";
        protected const string IdColumnName = "Id";

        private void UpdatePrefixList(SPPrefixCollection prefixes)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Inclusion", typeof(string)));
            dataTable.Columns.Add(new DataColumn("PathName", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Comment", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Id", typeof(string)));
            int num = 0;
            foreach (SPPrefix sPPrefix in prefixes)
            {
                if (sPPrefix.PrefixType == SPPrefixType.WildcardInclusion || sPPrefix.PrefixType == SPPrefixType.ExplicitInclusion)
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["Inclusion"] = SPHttpUtility.NoEncode(sPPrefix.Name);
                    if (sPPrefix.PrefixType == SPPrefixType.WildcardInclusion)
                    {
                        dataRow["Comment"] = SPHttpUtility.NoEncode(SPResource.GetString("WildcardInclusion", new object[0]));
                    }
                    else
                    {
                        dataRow["Comment"] = SPHttpUtility.NoEncode(SPResource.GetString("ExplicitInclusion", new object[0]));
                    }
                    if (sPPrefix.Name == "")
                    {
                        dataRow["PathName"] = SPHttpUtility.NoEncode(SPResource.GetString("RootNone", new object[0]));
                    }
                    else
                    {
                        dataRow["PathName"] = SPHttpUtility.NoEncode(sPPrefix.Name);
                    }
                    dataRow["Id"] = SPHttpUtility.NoEncode("idInclusion" + num);
                    num++;
                    dataTable.Rows.Add(dataRow);
                }
            }
            this.GvInclusions.DataSource = dataTable;
            this.GvInclusions.DataBind();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.TxtPathName.MaxLength = 55;
            this.BtnDeleteInclusion.Attributes["onclick"] = "return DelInclusionClick()";
            this.UpdatePrefixList(SPWebService.ContentService.HostHeaderPrefixes);
        }

        protected void BtnDeleteInclusion_Click(object sender, EventArgs e)
        {
            string[] values = base.Request.Params.GetValues("CkInclusionToDelete");
            if (values != null && values.Length != 0)
            {
                SPWebService.ContentService.HostHeaderPrefixes.Delete(values);
                this.UpdatePrefixList(SPWebService.ContentService.HostHeaderPrefixes);
                string strUrl = "managedpath.aspx";
                SPUtility.Redirect(strUrl, SPRedirectFlags.Static, this.Context);
            }
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            if (!base.IsValid)
            {
                return;
            }
            string text = this.TxtPathName.Text;
            text = text.Trim();
            SPPrefixCollection prefixes = SPWebService.ContentService.HostHeaderPrefixes;
            if (this.DdlInclude.SelectedItem.Value == "1")
            {
                prefixes.Add(text, SPPrefixType.WildcardInclusion);
            }
            else
            {
                prefixes.Add(text, SPPrefixType.ExplicitInclusion);
            }
            this.UpdatePrefixList(prefixes);
            this.TxtPathName.Text = "";
            string strUrl = "managedpath.aspx";
            SPUtility.Redirect(strUrl, SPRedirectFlags.Static, this.Context);
        }

        protected void BtnDone_Click(object sender, EventArgs e)
        {
            base.EndOperation(0);
        }
    }
}
